"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 兼容性
Case Name   : 修改失败后查看pg_log中是否暴露b_mode_create_user_if_not_exist函数
Create At   : 2023/06/06
@zou_jialiang0501129488
Description :
    1、创建兼容b库并查询插件是否加载成功
    2、修改失败后查看pg_log中是否暴露b_mode_create_user_if_not_exist函数
    3、清理环境
Expect      :
    1、成功
    2、审计日志中无明文显示密码
    3、成功
History     :
"""
import os
import time
import unittest

from yat.test import Node
from yat.test import macro

from testcase.utils.Common import Common
from testcase.utils.Common import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Tools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.common = Common()
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.pri_node = Node('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = 'db_grant_case023'
        self.u_name = 'u_grant_case023'

    def test_grant(self):
        text = '-----step1:创建兼容b库库并查询插件;expect:成功-----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} dbcompatibility ='B';"
        self.log.info(sql_cmd)
        sql_res = self.pri_sh.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS,
                      sql_res, '兼容B库创建失败')

        sql_cmd1 = "select extname from pg_extension where extname ='dolphin';"
        self.log.info(sql_cmd1)
        sql_res1 = self.pri_sh.execut_db_sql(sql_cmd1, dbname=self.db_name)
        self.log.info(sql_res1)
        self.assertIn('dolphin', sql_res1, '查询失败')

        text = '-----step2:修改失败后查看pg_log中是否暴露系统函数;expect:无明文密码显示-----'
        self.log.info(text)
        sql_cmd = f"show LOG_DIRECTORY;"
        sql_res = self.pri_sh.execut_db_sql(sql_cmd, dbname=self.db_name)
        self.log.info(sql_res)
        self.assertIn('(1 row)', sql_res, '查询失败')
        pg_log = sql_res.splitlines()[-2].strip()
        self.log.info(pg_log)

        sql_cmd1 = f"drop user if exists {self.u_name} cascade;" \
                   f"grant usage on *.* to {self.u_name} " \
                   f"identified by '{macro.COMMON_PASSWD}';" \
                   f"grant usage on *.* to {self.u_name} " \
                   f"identified by '{macro.COMMON_PASSWD}';"
        sql_res1 = self.pri_sh.execut_db_sql(sql_cmd1, dbname=self.db_name)
        self.log.info(sql_res1)
        self.assertIn(self.constant.ALTER_ROLE_SUCCESS_MSG, sql_res1, 'grant失败')
        err_msg = 'ERROR:  New password should not equal to the old ones'
        self.assertIn(err_msg, sql_res1, 'grant失败')
        time.sleep(5)

        cmd2 = f"cd {pg_log};" \
               f"ls -t | head -1 | awk '{{{{print $1}}}}' " \
               f"| xargs cat | tail -30 | " \
               f"grep 'b_mode_create_user_if_not_exist'"
        self.log.info(cmd2)
        res2 = self.common.get_sh_result(self.pri_node, cmd2)
        self.log.info(res2)
        self.assertFalse(res2, 'pg_log中有明文密码显示')

    def tearDown(self):
        text = "-----step3:清理环境;expect:成功-----"
        self.log.info(text)
        sql_cmd3 = f"drop database if exists {self.db_name};" \
                   f"drop user if exists {self.u_name} cascade;"
        sql_res3 = self.pri_sh.execut_db_sql(sql_cmd3)
        self.log.info(sql_res3)
        self.log.info(f'-----{os.path.basename(__file__)} finish-----')
        self.assertIn(self.constant.DROP_ROLE_SUCCESS_MSG, sql_res3, '用户删除失败')
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, sql_res3, '数据库删除失败')
